const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: app.globalData.userInfo,
    setting: app.globalData.setting,
    navHeight: app.globalData.navHeight,
    capsule: app.globalData.capsule,
    mch: {},
    users: [],
    showmodal: false,
    showtab: 0,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      setting: app.globalData.setting,
    })
  },
  onShow() {
    this.getUsers()
    this.getUserInfo()
    this.getMchInfo()
  },
  getUserInfo() {
    wx.cloud.callFunction({
      name: 'user',
    }).then(res => {
      console.log('mine.userInfo', res.result)
      app.globalData.userInfo = res.result
      wx.setStorageSync('userInfo', res.result)
      let sinceToday = app.util.sinceToday(res.result.register_time)
      this.setData({
        userInfo: res.result,
        sinceToday: sinceToday,
      })

    })
  },
  getUsers() {

    wx.cloud.callFunction({
      name: 'admin',
      data: {
        op: 'getUsers'
      }
    }).then(res => {
      console.log('users', res)
      this.setData({
        users: res.result.list,
        total: res.result.total
      })
    })

  },
  delUser(e) {
    wx.cloud.callFunction({
      name: 'admin',
      data: {
        op: 'delUser',
        _id: e.currentTarget.dataset.id
      }
    }).then(res => {
      console.log('delUser', res)
      this.getUsers()
    })
  },
  updateUserInfo: function (e) {
    let that = this
    console.log(e)
    // 拒绝授权
    if (e.detail.errMsg == "getUserInfo:fail auth deny") {
      wx.showModal({
        title: '请授权',
        content: '否则您无法继续使用',
        showCancel: false
      });
      return;
    }
    console.log('原始用户信息：', e.detail.userInfo)
    let userInfo = e.detail.userInfo

    wx.cloud.callFunction({
      name: 'user',
      data: {
        op: 'updateUserInfo',
        userInfo: userInfo
      }
    }).then((res) => {
      console.log(res)
      this.getUserInfo()
    })

  },
  updateNickname(e) {
    console.log(e.detail.value)
    wx.cloud.callFunction({
      name: 'user',
      data: {
        op: 'updateNickname',
        nickname: e.detail.value.nickname
      }
    }).then((res) => {
      if (res.result.code == "200") {
        //检测通过
        wx.showToast({
          title: '修改成功',
          icon: 'none',
          duration: 3000
        })
        this.setData({
          showmodal: false
        })
        this.getUserInfo()
      } else {

        //执行不通过
        wx.showToast({
          title: '包含敏感字哦。',
          icon: 'none',
          duration: 3000
        })
      }
    })


  },
  getMchInfo() {
    wx.cloud.callFunction({
      name: 'merchant',
      data: {
        op: 'getMchInfo',
      }
    }).then((res) => {
      console.log(res.result)
      this.setData({
        mch: res.result
      })
    })
  },
  delRecord() {
    wx.showModal({
      content: '是否需要删除这些消息？',
      confirmText: '删除',
      cancelText: '不删除',
      success(res) {
        if (res.confirm) {
          wx.cloud.callFunction({
            name: 'getRecord',
            data: {
              op: 'delRecord',
            }
          }).then(res => {
            console.log('del-records', res.result)

          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 下拉刷新
   */

  changeTab(e) {
    this.setData({
      showtab: e.currentTarget.dataset.index
    })
    // if (e.currentTarget.dataset.index == 1) {
    //   this.loadMyTreasure();
    // }
  },
  /**
   * 点击modal空白区域
   */
  modalClose: function () {
    this.setData({
      showmodal: false
    });
  },

  showModal(e) {
    this.setData({
      showmodal: e.currentTarget.dataset.id
    });
  },


})